using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using objetos;
using dados;
using System.Web.UI;


namespace negocios
{
    
	#region TB_MENU
	/// <summary>
	/// This object represents the properties and methods of a TB_MENU.
	/// </summary>
	public class BLL_TB_MENU
	{		
		DAL_TB_MENU DAL = new DAL_TB_MENU();
		
		#region METODOS PROCEDURES
		/// <summary>
		/// Incluir 
		/// </summary>
		public int Incluir(OBJ_TB_MENU obj)
		{
			return	DAL.Incluir(obj);
			
		}
		
		/// <summary>
		/// Alterar 
		/// </summary>
		public void Alterar(OBJ_TB_MENU obj)
		{
			DAL.Alterar(obj);	
		}
		
		/// <summary>
		/// Excluir 
		/// </summary>
		public void Excluir(OBJ_TB_MENU obj)
		{
			DAL.Excluir(obj);	
		}
		
		/// <summary>
		/// Excluir Lista 
		/// </summary>
		public void ExcluirLista(List<OBJ_TB_MENU> lista)
		{
				foreach (OBJ_TB_MENU obj in lista)
            	{
                this.Excluir(obj);
            	}
			
		}
						
		/// <summary>
		/// Pesquisar Parametros Where, OrderBy, PageIndex, PageSize
		/// </summary>		
		public DataSet Selecionar(string Where, string OrderBy, int PageIndex, int PageSize)
		{
			
			return DAL.Selecionar(Where,OrderBy,PageIndex,PageSize);
			
		}		
		
		
		/// <summary>
		/// Retorna Objeto por ID
		/// </summary>
		public OBJ_TB_MENU SelecionarObjetoPorId(int id)
		{
			return DAL.SelecionarObjetoPorId(id);
			
		}
				
			
		/// <summary>
		/// Retorna DataSet Parametros Where, OrderBy
		/// </summary>	
		public DataSet Selecionar(String Where, String OrderBy)
        {
            return DAL.Selecionar(Where, OrderBy);
        }
		
		/// <summary>
		/// Retorna DataSet
		/// </summary>	
		public DataSet Selecionar()
        {
            return DAL.Selecionar();
        }
			
		
		/// <summary>
		/// Retorna uma lista de objetos por parametro Where, OrderBy
		/// </summary>
		public List<OBJ_TB_MENU>SelecionarLista(String Where, String OrderBy)
		{						
			return 	DAL.SelecionarLista(Where,OrderBy);
		}
		
		
		/// <summary>
		/// Retorna uma lista de objetos por parametro 
		/// </summary>
		public List<OBJ_TB_MENU>SelecionarLista()
		{						
			return 	DAL.SelecionarLista();
		}

        public List<OBJ_TB_MENU> ListaMenuPorIdUsuario(int id_usuario)
        {

            return DAL.ListaMenuPorIdUsuario(id_usuario);

        }

        public void ListaMenu(System.Web.UI.Control controle, bool selecione)
        {
            if (controle is DropDownList)
            {
                List<OBJ_TB_MENU> lista = new List<OBJ_TB_MENU>();
                DropDownList ddl = (DropDownList)controle;
                ddl.Items.Clear();

                foreach (OBJ_TB_MENU o_tb_menu in DAL.DropDownList())
                {
                    ddl.Items.Add(new ListItem(o_tb_menu.menu.ToString(),o_tb_menu.id.ToString()));                    
                }                

                if (selecione)
                {
                    ddl.Items.Insert(0, new ListItem("Selecione", ""));
                    ddl.SelectedIndex = 0;
                }

                ddl.DataBind();
            }

            if (controle is ListBox)
            {
                ListBox lst = (ListBox)controle;
                lst.Items.Clear();
                foreach (OBJ_TB_MENU obj in DAL.DropDownList())
                {
                    lst.Items.Add(new ListItem(obj.menu.ToString(), obj.id.ToString()));
                }

                if (selecione)
                {
                    lst.Items.Insert(0, new ListItem("Selecione", ""));
                    lst.SelectedIndex = 0;
                }

                lst.DataBind();
            }

            if (controle is CheckBoxList)
            {

                CheckBoxList chk = (CheckBoxList)controle;
                chk.Items.Clear();
                foreach (OBJ_TB_MENU obj in DAL.DropDownList())
                {
                   chk.Items.Add(new ListItem(obj.menu.ToString(), obj.id.ToString()));
                }

                if (selecione)
                {
                    chk.Items.Insert(0, new ListItem("Selecione", ""));
                    chk.SelectedIndex = 0;
                }

                chk.DataBind();

            }

           
        }
		
		public void PreencheControle(System.Web.UI.Control controle, bool selecione)
        {
            if (controle is DropDownList)
            {
                DropDownList ddl = (DropDownList)controle;
                ddl.Items.Clear();
                foreach (OBJ_TB_MENU obj in DAL.SelecionarLista())
                {
                    ddl.Items.Add(new ListItem(obj.menu.ToString(), obj.id.ToString()));
                }
                
                if (selecione)
                {
                    ddl.Items.Insert(0, new ListItem("Selecione", ""));
                    ddl.SelectedIndex = 0;
                }

                ddl.DataBind();
            }


            if (controle is ListBox)
            {
                ListBox lst = (ListBox)controle;
                lst.Items.Clear();
                foreach (OBJ_TB_MENU obj in DAL.SelecionarLista())
                {
                    lst.Items.Add(new ListItem(obj.menu.ToString(), obj.id.ToString()));
                }

                if (selecione)
                {
                    lst.Items.Insert(0, new ListItem("Selecione", ""));
                    lst.SelectedIndex = 0;
                }

                lst.DataBind();
            }

        }										
			
		
		public DataSet ListaCampos()
		{
			 return DAL.ListaCampos();
		}

        public OBJ_TB_MENU SelecionarObjetoPorIdMenuPai(int id)
        {
            return DAL.SelecionarObjetoPorIdMenuPai(id);

        }



        public Control ExibeMenu(Int32 id_usuario)
        {
            List<OBJ_TB_MENU> lista = new List<OBJ_TB_MENU>();
            lista = DAL.ListaMenuPai(id_usuario);

            Control ctrl = new Control();
            Literal ulA = new Literal();
            Literal ulF = new Literal();

            ulA.Text = "<ul id='navigation'>";
            ctrl.Controls.Add(ulA);
            foreach (OBJ_TB_MENU obj in lista)
            {
                Literal liA = new Literal();
                Literal liF = new Literal();
                liA.Text = "<li>";
                ctrl.Controls.Add(liA);

                HyperLink link = new HyperLink();
                link.Text = obj.menu.ToString();
                if(obj.parametro.ToString() != string.Empty)
                {
                    link.NavigateUrl = obj.url.ToString() +"?"+ obj.parametro.ToString();
                }
                else
                {
                    link.NavigateUrl = obj.url.ToString();
                }
                
                ctrl.Controls.Add(link);

                ctrl.Controls.Add(ExibeSubMenu(obj.id, id_usuario)); // Cria submenu;

                liF.Text = "</li>";
                ctrl.Controls.Add(liF);



            }
            ulF.Text = "</ul>";
            ctrl.Controls.Add(ulF);
            return ctrl;
        }

        private Control ExibeSubMenu(Int32 id_menu, Int32 cod_usuario)
        {
            List<OBJ_TB_MENU> lista = new List<OBJ_TB_MENU>();
            lista = DAL.ListaMenuFilho(id_menu, cod_usuario);

            Control ctrl = new Control();
            Literal ulA = new Literal();
            Literal ulF = new Literal();

            if (lista.Count > 0)
                ulA.Text = "<ul>";
            ctrl.Controls.Add(ulA);
            foreach (OBJ_TB_MENU obj in lista)
            {
                Literal liA = new Literal();
                Literal liF = new Literal();
                liA.Text = "<li>";
                ctrl.Controls.Add(liA);

                HyperLink link = new HyperLink();
                link.Text = obj.menu.ToString();
                if (obj.parametro.ToString() != string.Empty)
                {
                    link.NavigateUrl = obj.url.ToString() + "?" + obj.parametro.ToString();
                }
                else
                {
                    link.NavigateUrl = obj.url.ToString();
                }
                ctrl.Controls.Add(link);

                ctrl.Controls.Add(ExibeNeto(obj.id, cod_usuario)); // Cria submenu;

                liF.Text = "</li>";
                ctrl.Controls.Add(liF);



            }
            if (lista.Count > 0)
                ulF.Text = "</ul>";
            ctrl.Controls.Add(ulF);
            return ctrl;
        }

        private Control ExibeNeto(Int32 id_menu, Int32 cod_usuario)
        {
            List<OBJ_TB_MENU> lista = new List<OBJ_TB_MENU>();
            lista = DAL.ListaMenuFilho(id_menu, cod_usuario);

            Control ctrl = new Control();
            Literal ulA = new Literal();
            Literal ulF = new Literal();
            if (lista.Count > 0)
                ulA.Text = "<ul>";
            ctrl.Controls.Add(ulA);
            foreach (OBJ_TB_MENU obj in lista)
            {
                Literal liA = new Literal();
                Literal liF = new Literal();
                liA.Text = "<li>";
                ctrl.Controls.Add(liA);

                HyperLink link = new HyperLink();
                link.Text = obj.menu.ToString();
                if (obj.parametro.ToString() != string.Empty)
                {
                    link.NavigateUrl = obj.url.ToString() + "?" + obj.parametro.ToString();
                }
                else
                {
                    link.NavigateUrl = obj.url.ToString();
                }
                ctrl.Controls.Add(link);
                liF.Text = "</li>";
                ctrl.Controls.Add(liF);



            }
            if (lista.Count > 0)
                ulF.Text = "</ul>";
            ctrl.Controls.Add(ulF);
            return ctrl;
        }


        public void PreencheTreeView(TreeView tree, bool collapseall, bool checkbox)
        {
            //            subNode.ShowCheckBox = false;

            //            subNode.CollapseAll();


            foreach (OBJ_TB_MENU o_tb_menu in DAL.SelecionarLista("id_menu_pai = 0", "id_menu_pai"))
            {
                TreeNode nodePai = new TreeNode(o_tb_menu.menu.ToString(), o_tb_menu.id.ToString());

                nodePai.ShowCheckBox = checkbox;


                foreach (OBJ_TB_MENU o_tb_menu_filho in DAL.SelecionarLista("id_menu_pai = " + o_tb_menu.id.ToString(), "id_menu_pai"))
                {
                    TreeNode nodeFilho = new TreeNode(o_tb_menu_filho.menu.ToString(), o_tb_menu_filho.ToString());
                    nodeFilho.ShowCheckBox = checkbox;
                    foreach (OBJ_TB_MENU o_tb_menu_neto in DAL.SelecionarLista("id_menu_pai = " + o_tb_menu_filho.id.ToString(), "id_menu_pai"))
                    {
                        TreeNode nodeNeto = new TreeNode(o_tb_menu_neto.menu.ToString(), o_tb_menu_neto.id.ToString());
                        nodeNeto.ShowCheckBox = checkbox;
                        nodeFilho.ChildNodes.Add(nodeNeto);
                       
                    }

                    nodePai.ChildNodes.Add(nodeFilho);
          
                    if (collapseall) {  nodePai.CollapseAll();

                    }
                    else{                        
                        nodePai.ExpandAll();
                    }

                }



                tree.Nodes.Add(nodePai);

            }
        }


        //public void ListaMenuTreeView(Int32 id, TreeNode node)
        //{

        //    List<OBJ_TB_MENU> lista = new List<OBJ_TB_MENU>();

            

        //    lista = DAL.SelecionarListaPorIdMenuPai(id);

        //    if (lista != null)
        //    {
        //        for (int i = 0; i < lista.Count; i++)
        //        {


        //            TreeNode subNode = new TreeNode(lista[i].menu.ToString(), lista[i].id.ToString());

        //            subNode.ShowCheckBox = false;

        //            subNode.CollapseAll();

        //            node.ChildNodes.Add(subNode);

        //            // bind sub directories

        //            ListaMenuTreeView(Convert.ToInt32(lista[i].id.ToString()), subNode);
        //        }
        //    }



        //}

		#endregion
	}
	#endregion
}

